package jfilters.lib;

/*
    Copyright Janne Liljeblad 2008.

    This file is part of JFilters.

    JFilters is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    JFilters is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with JFilters.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.image.*;

//--- This class is used to simulate a guchar* buffer from method void gimp_pixel_rgn_get_row (...)
public class PixelRow
{
	//--- Edge action in PixelRow 
	public static int FREE_EDGES = 0;
	public static int CLAMP_EDGES = 1;
	//public static int WRAP_EDGES = 2;

	private WritableRaster raster;
	private int y;
	private int[] data;
	private int edgeAction;
	private int bytesPerPix;

	public PixelRow( WritableRaster raster, int row )
	{
		this( raster, row, 0 );
	}

	public PixelRow( WritableRaster raster, int row, int edgeAction )
	{
		this.raster = raster;
		this.edgeAction = edgeAction;
		y = row;
		bytesPerPix = raster.getNumBands();
		data = new int[ bytesPerPix * raster.getWidth() ];
		raster.getPixels( 0, row, raster.getWidth(), 1, data );
		edgeAction = FREE_EDGES;
	}

	public void setSample( int offset, int sample )
	{
		data[ offset ] = sample;
	}

	//--- Puts value of sample into array
	public int getSample( int offset )
	{
		if ( offset >= 0 && offset < data.length ) return data[ offset ];
		else return 0;
		/*
		else if( edgeAction == CLAMP_EDGES )
		{
			if( offset < 0 ) return data[ bytesPerPix - ((-offset) % bytesPerPix ) - 1 ];// same channel from pix( x = 0 )
			else return data[ data.length - offset          ;// same channel from pix( x = width - 1 )
		}
		else
		{
			System.out.println("pixel out of range");
		}

		/*
		return 0;
		*/
	}

	public void commitData()
	{
		raster.setPixels( 0, y, raster.getWidth(), 1, data );
	}

}//end class